package com.y.architecture.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.y.architecture.R;
import com.y.architecture.util.AFConstant;
import com.y.architecture.util.AFLog;
import com.y.architecture.util.AFPermissionUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import org.litepal.tablemanager.Connector;
/**
 * Created by youjingjing on 2017/11/15.
 */

public abstract class BaseActivity extends AppCompatActivity implements IBaseContact.IBaseView{
    protected FragmentActivity mActivity;
    private BaseHelper mBaseHelper;
    private Unbinder bind;
    private ProgressDialog mProgressDialog;
    private IBaseContact.IRequestPermissionCallback mPermissionCallback;
    SharedPreferences pref;
    SharedPreferences.Editor share_edit;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        bind = ButterKnife.bind(this);
        this.mActivity = getAFContext();
        mBaseHelper = new BaseHelper(this);
        pref =  getSharedPreferences("Info", Context.MODE_PRIVATE);
        share_edit = pref.edit();
        init(savedInstanceState);
        initViews(savedInstanceState);
    }

    @Override
    public FragmentActivity getAFContext() {
        return this;
    }

    @Override
    public void init(Bundle savedInstanceState) {

    }

    @Override
    public void controlStatusBar() {
    }

    @Override
    public void showToast(String message) {
        if (TextUtils.isEmpty(message)) {
            return;
        }
        int during = Toast.LENGTH_SHORT;
        if (message.length() > AFConstant.TOAST_LONG_MESSAGE_LENGTH) {
            during = Toast.LENGTH_LONG;
        }
        Toast.makeText(mActivity, message, during).show();
    }
    public void writeBoolean(String a, boolean boo) {
        share_edit.putBoolean(a, boo);
        share_edit.commit();
    }

    public void writeString(String a, String s) {
        share_edit.putString(a, s);
        share_edit.commit();
    }

    public String readData(String data) {
        String str = pref.getString(data, "");
        return str;
    }

    public boolean readBoolean(String data) {
        return pref.getBoolean(data, false);
    }

    public void isFirstUse() {
        if(pref.getBoolean("isFirst",true)){
//            Toast.makeText(this, "首次使用app", Toast.LENGTH_SHORT).show();
            share_edit.putBoolean("isFirst",false);
            share_edit.commit();
            SQLiteDatabase database = Connector.getDatabase();
        }else{
//            Toast.makeText(this, "再次开启", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void showProgressDialog(String message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mActivity);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setCancelable(true);
        }
        mProgressDialog.setMessage(message != null ? message : "");
        mProgressDialog.show();
    }

    @Override
    public void hideProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void startActivity(Class clazz, Bundle bundle) {
        Intent intent = new Intent(getAFContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.af_right_in, 0);
    }

    @Override
    public void startActivityForResult(Class clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getAFContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
         startActivityForResult(intent, requestCode);
         overridePendingTransition(R.anim.af_right_in, 0);
    }

    @Override
    public void requestPermission(IBaseContact.IRequestPermissionCallback permissionCallback, String reqStr, String... permissions) {
        mPermissionCallback = permissionCallback;
        if (AFPermissionUtil.hasPermissions(mActivity, permissions)) {
            onPermissionsAllGranted();
        } else {
            AFPermissionUtil.requestPermissions(mActivity, reqStr, AFConstant.PERMISSION_REQUEST_CODE, permissions);
        }
    }
    /**
     * 用户权限处理,
     * 如果全部获取, 则直接过.
     * 如果权限缺失, 则提示Dialog.
     *
     * @param requestCode  请求码
     * @param permissions  权限
     * @param grantResults 结果
     */
    /*OnRequestPermissionsResultCallback
    *    onRequestPermissionsResult
    * */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        AFPermissionUtil.onRequestPermissionsResult(requestCode, permissions, grantResults, mActivity);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (isPermissionRequestBackFromSetting(requestCode)) {
            permissionRequestBackFromSetting();
        }
    }

    /**
     * 这个只处理权限请求从设置界面返回，这时候应该再次调用请求权限部分的方法
     */
    protected void permissionRequestBackFromSetting() {
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.af_right_out);
    }
    /*权限requestCode设置 PERMISSION_OPEN_SETTING_REQUEST_CODE = 10002*/
    public boolean isPermissionRequestBackFromSetting(int requestCode) {
        return requestCode == AFConstant.PERMISSION_OPEN_SETTING_REQUEST_CODE;
    }

    /*PermissionCallbacks
    *    onPermissionsGranted
    *    onPermissionsAllGranted
    *    onPermissionsDenied
    * */
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        // 只同意了部分权限
    }

    @Override
    public void onPermissionsAllGranted() {
        if (mPermissionCallback != null) {
            mPermissionCallback.agreeAll();//同意了全部权限的回调
            AFLog.d("request permission all agreed");
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        AFPermissionUtil.checkDeniedPermissionsNeverAskAgain(mActivity,
                 getString(R.string.af_permission_tips), getString(R.string.setting),
                getString(R.string.cancel), perms);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBaseHelper.destroyIt();
        bind.unbind();
        mActivity = null;
    }

}
